package cn.zkrt.iotms.module.iot.service.impl;

import cn.zkrt.iotms.module.iot.dao.StationDeviceBDao;
import cn.zkrt.iotms.module.iot.entity.StationDeviceB;
import cn.zkrt.iotms.module.iot.service.StationDeviceBService;
import cn.zkrt.iotms.module.iot.vo.StationDeviceBVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 站点设备(StationDeviceB)表服务实现类
 *
 * @author xulinyu
 * @since 2024-10-06 16:29:39
 */
@Service("stationDeviceBService")
public class StationDeviceBServiceImpl extends ServiceImpl<StationDeviceBDao,StationDeviceB> implements StationDeviceBService {
    @Resource
    private StationDeviceBDao stationDeviceBDao;

    @Override
    public Page<StationDeviceBVO> queryByPage(Page<StationDeviceBVO> page, String deviceName, String stcd) {
        return stationDeviceBDao.queryByPage(page,deviceName,stcd);
    }

    @Override
    public Boolean add(StationDeviceB stationDeviceB) {
        verifyDuplicate(stationDeviceB);
        return this.save(stationDeviceB);
    }

    @Override
    public Boolean edit(StationDeviceB stationDeviceB) {
        verifyDuplicate(stationDeviceB);
        return this.updateById(stationDeviceB);
    }

    private void verifyDuplicate(StationDeviceB stationDeviceB) {
        LambdaQueryWrapper<StationDeviceB> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(StringUtils.isNotEmpty(stationDeviceB.getId()),StationDeviceB::getId,stationDeviceB.getId());
        wrapper.eq(StringUtils.isNotEmpty(stationDeviceB.getDeviceId()),StationDeviceB::getDeviceId,stationDeviceB.getDeviceId());
        wrapper.eq(StringUtils.isNotEmpty(stationDeviceB.getStcd()),StationDeviceB::getStcd,stationDeviceB.getStcd());
        Integer count = stationDeviceBDao.selectCount(wrapper);
        if(count > 0) {
            throw new RuntimeException("站点设备已存在");
        }
    }
}
